package com.grasscove.namethat;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;

import com.grasscove.namethat.adapters.WordsDbAdapter;
import com.grasscove.namethat.adapters.WordsListAdapter;
import com.grasscove.namethat.models.Word;
import com.grasscove.namethat.quickaction.ActionItem;
import com.grasscove.namethat.quickaction.QuickAction;

public class WordsList extends ListActivity {

	public static final int EDIT_WORD = 0;
	public static final int DELETE_WORD = 1;

	public static final int DIALOG_CONFIRM_DELETE = 0;
	public static final int DIALOG_EDIT_WORD = 1;
	public static final int DIALOG_ADD_WORD = 2;

	public static final int OPTION_MENU_ADD = 0;

	private WordsDbAdapter mDbAdapter;
	private SimpleCursorAdapter mWords;
	private Cursor mCursor;
	private Word word;
	private EditText editedWord;

	private ActionItem mEditActionItem = null;
	private ActionItem mDeleteActionItem = null;
	private QuickAction mQuickAction = null;

	private int mCategoryId;
	private float mSeectedWordId;
	private String mSelectedWord;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent i = getIntent();
		mCategoryId = i.getIntExtra("category", 1);

		mDbAdapter = new WordsDbAdapter(this);
        mDbAdapter.open();

        mCursor = mDbAdapter.getAllFromCategory(mCategoryId);
        //startManagingCursor(mCursor);

        String[] from = new String[] {};
        int[] to = new int[] {};

        mWords = new WordsListAdapter(this, R.layout.words_row, mCursor, from, to);

        setContentView(R.layout.categories);

        mEditActionItem = new ActionItem();
		mEditActionItem.setTitle("Edit");
		mEditActionItem.setIcon(getResources().getDrawable(R.drawable.edit_icon));
		mEditActionItem.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(DIALOG_EDIT_WORD);
			}
		});

		mDeleteActionItem = new ActionItem();
		mDeleteActionItem.setTitle("Delete");
		mDeleteActionItem.setIcon(getResources().getDrawable(R.drawable.delete_icon));
		mDeleteActionItem.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(DIALOG_CONFIRM_DELETE);
			}
		});
        
		setListAdapter(mWords);
		registerForContextMenu(getListView());
	}

	@Override
	protected void onPause() {
		super.onPause();

		//mCursor.close();
	}

	@Override
	protected void onResume() {
		super.onResume();

		mCursor.requery();
	}



	@Override
	protected void onDestroy() {
		super.onDestroy();

		mCursor.close();
		mDbAdapter.close();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {

		mQuickAction = new QuickAction(v, position);

		mSeectedWordId = (float) getListAdapter().getItemId(position);
        mSelectedWord = (String) getListAdapter().getItem(position);

		mQuickAction.addActionItem(mEditActionItem);
		mQuickAction.addActionItem(mDeleteActionItem);
		mQuickAction.setAnimStyle(QuickAction.ANIM_AUTO);

		mQuickAction.show();
	}

	/*
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);

        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        mSeectedWordId = (float) getListAdapter().getItemId(info.position);
        mSelectedWord = (String) getListAdapter().getItem(info.position);

        menu.setHeaderTitle(mSelectedWord);
        menu.add(0, EDIT_WORD, 0, "Edit");
        menu.add(0, DELETE_WORD, 1, "Delete");
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

        switch (item.getItemId()) {
            case EDIT_WORD:
            	showDialog(DIALOG_EDIT_WORD);
                return true;
            case DELETE_WORD:
                showDialog(DIALOG_CONFIRM_DELETE);
                return true;
            default:
                return super.onContextItemSelected(item);
        }
	}
	*/

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, OPTION_MENU_ADD, 1, "Add").setIcon(android.R.drawable.ic_menu_add);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case OPTION_MENU_ADD:
				showDialog(DIALOG_ADD_WORD);
				break;
			default:
				break;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
    protected Dialog onCreateDialog(int id) {
		mQuickAction.dismiss();

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        Dialog dialog = new Dialog(this);
        View layout;

		LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);

        switch (id) {
            case DIALOG_CONFIRM_DELETE:
                builder.setCancelable(true).setTitle("Confirm").setMessage("Are you sure you want to delete that word?")
                .setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            mDbAdapter.delete((int)mSeectedWordId);
                            mCursor.requery();
                        }
                    }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                    	public void onClick(DialogInterface dialog, int id) {
                    	}
                    });
                return builder.create();
            case DIALOG_EDIT_WORD:
            	layout = inflater.inflate(R.layout.words_row_edit, null);

            	word = mDbAdapter.getRowByWord(mSelectedWord);

    			editedWord = (EditText)layout.findViewById(R.id.word_edit);
    			editedWord.setText(mSelectedWord);

    			builder
    				.setTitle("Edit word")
    				.setPositiveButton("Save", new DialogInterface.OnClickListener() {
    					public void onClick(DialogInterface dialog, int id) {
    						word.setWord(editedWord.getText().toString());
    						mDbAdapter.update(word);
    						mCursor.requery();
    					}
    				})
    			    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    			    	public void onClick(DialogInterface dialog, int id) {
    			    	}
    			    });
    			builder.setView(layout);

    			return builder.create();
            case DIALOG_ADD_WORD:
            	layout = inflater.inflate(R.layout.words_row_edit, null);

            	word = new Word();

            	editedWord = (EditText)layout.findViewById(R.id.word_edit);

    			builder
    				.setTitle("Add word")
    				.setPositiveButton("Save", new DialogInterface.OnClickListener() {
    					public void onClick(DialogInterface dialog, int id) {
    						word.setWord(editedWord.getText().toString());
    						word.setCategoryId(mCategoryId);
    						mDbAdapter.insert(word);
    						mCursor.requery();
    					}
    				})
    			    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    			    	public void onClick(DialogInterface dialog, int id) {
    			    	}
    			    });
    			builder.setView(layout);

    			return builder.create();
            default:
                return null;
        }
    }
}